package com.example.myapplication.module1.activity

import android.view.View
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.TimePickerView
import com.example.myapplication.R
import com.example.myapplication.databinding.ActivityScheduleDetailBinding
import com.example.myapplication.entity.ScheduleBean
import com.example.myapplication.module1.presenter.ScheduleDetailPresenter
import com.example.myapplication.soaprequest.SoapEnvelop
import com.example.myapplication.soaprequest.request.RcpromptDetailRequest
import com.example.myapplication.soaprequest.request.RcpromptSaveRequest
import com.example.myapplication.soaprequest.request.SoapRequestBody
import com.example.myapplication.widget.CustomerToolbar
import com.example.mylibrary.base.BaseMvpActivity
import com.example.mylibrary.utils.GsonUtil
import com.example.mylibrary.utils.UiTools
import com.google.gson.reflect.TypeToken
import com.lxj.xpopup.impl.LoadingPopupView
import java.text.SimpleDateFormat
import java.util.*


/**
 * 个人日程详细信息
 */
class ScheduleDetailActivity :
    BaseMvpActivity<ScheduleDetailActivity, ScheduleDetailPresenter, ActivityScheduleDetailBinding>() {
    override fun getPresenter(): ScheduleDetailPresenter = ScheduleDetailPresenter()
    private var scheduleBean: ScheduleBean? = null
    private var dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
    private val rcpromptDetailRequest = RcpromptDetailRequest()
    private val rcpromptSaveRequest = RcpromptSaveRequest()
    private val soapRequestBody = SoapRequestBody()
    private val soapEnvelop = SoapEnvelop()
    lateinit var loadPopup: LoadingPopupView
    private var pvTime: TimePickerView? = null

    override fun getBinding(): ActivityScheduleDetailBinding {
        return ActivityScheduleDetailBinding.inflate(layoutInflater)
    }

    override fun initData() {
        scheduleBean = bundleExtra?.getParcelable<ScheduleBean>("scheduleBean")
        scheduleBean?.let { getScheduleDetail() }

    }

    private fun getScheduleDetail() {
        rcpromptDetailRequest.id = scheduleBean?.id.toString()
        soapRequestBody.value = rcpromptDetailRequest
        soapEnvelop.body = soapRequestBody
        mPresenter?.getScheduleDetail(soapEnvelop)
    }

    override fun initView() {
        loadPopup = builder.asLoading("加载中...")
        viewBinding.clToolbar.setTitle(getString(R.string.personSchedule))
        viewBinding.clToolbar.clickListener = object : CustomerToolbar.ClickListener {
            override fun onBackListener() {
                finish()
            }

            override fun onRightListener() {
                // 保存
                val title = viewBinding.etTitle.text
                val content = viewBinding.etContent.text
                if (title.isEmpty()) {
                    UiTools.showToast(R.string.inputTitle)
                    return
                }
                if (content.isEmpty()) {
                    UiTools.showToast(R.string.inputContent)
                    return
                }
                if (viewBinding.cbNotice.isChecked) {
                    val startTime = viewBinding.etStartTime.text
                    if (startTime.isEmpty()) {
                        UiTools.showToast(getString(R.string.chooseStartTime))
                        return
                    }
                }
                saveScheduleDetail()
            }
        }
        viewBinding.etStartTime.setOnClickListener(this)
        viewBinding.etStartTime.isEnabled = viewBinding.cbNotice.isChecked
        viewBinding.cbNotice.setOnCheckedChangeListener { _, isChecked ->
            viewBinding.etStartTime.isEnabled = isChecked
            if (!isChecked) {
                viewBinding.etStartTime.setText("")
            }
        }


        //时间选择器
        pvTime = TimePickerBuilder(this) { date, _ ->
            viewBinding.etStartTime.setText(dateFormat.format(date))
        }
            .setType(booleanArrayOf(true, true, true, true, true, true))
            .build()
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.etStartTime -> {
                //时间选择器
                if (viewBinding.cbNotice.isChecked) {
                    pvTime?.show()
                }
            }
            else -> {
            }
        }
    }

    private fun saveScheduleDetail() {
        rcpromptSaveRequest.id = scheduleBean?.id ?: "0"
        rcpromptSaveRequest.title = viewBinding.etTitle.text.toString()
        rcpromptSaveRequest.info = viewBinding.etContent.text.toString()
        if (viewBinding.cbNotice.isChecked) {
            rcpromptSaveRequest.isused = "1"
            val time = viewBinding.etStartTime.text.toString()
            val split = time.split(" ")
            rcpromptSaveRequest.startdate = split[0]
            rcpromptSaveRequest.starttime = split[1]
        } else {
            rcpromptSaveRequest.isused = "0"
        }
        soapRequestBody.value = rcpromptSaveRequest
        soapEnvelop.body = soapRequestBody
        mPresenter?.saveScheduleDetail(soapEnvelop)
    }

    fun setScheduleDetail(result: String) {
        val scheduleBeanList = GsonUtil.fromJson<MutableList<ScheduleBean>>(
            result,
            object : TypeToken<MutableList<ScheduleBean>>() {}.type
        )

        if (!scheduleBeanList.isNullOrEmpty()) {
            val scheduleBean = scheduleBeanList[0]
            viewBinding.etTitle.setText(scheduleBean.title)
            viewBinding.etContent.setText(scheduleBean.info)
            when (scheduleBean.isused) {
                "0" -> {
                    // 不提醒
                    viewBinding.cbNotice.isChecked = false
                }
                "1" -> {
                    // 提醒
                    viewBinding.cbNotice.isChecked = true
                    viewBinding.etStartTime.setText("${scheduleBean.startdate} ${scheduleBean.starttime}")
                }
            }
        }
    }

    fun saveScheduleDetail(result: String) {
        UiTools.showToast(R.string.saveSuccess)
        setResult(RESULT_OK)
        finish()
    }

    fun showLoad() {
        loadPopup.show()
    }

    fun dismissLoad() {
        loadPopup.dismiss()
    }
}
